/* 
 * [New BSD License (BSD 3-Clause License)]
 * Copyright (c) 2012, Max Bechtold
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     - Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     - Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     - Neither the name of the Rel2Xml Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package maxbe.rel2xml.util;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

public final class Config {

	private static Logger log = Logger.getLogger(Config.class);
	private static Properties prop;

	public final static String VERBOSE = "generate.verbose";

	public final static String OMIT_NULLS = "doc.format.omitNulls";
	public final static String IMPLICIT_FKEYS = "doc.format.omitImplicitForeignKeys";
	public final static String PRETTY_PRINT = "doc.format.prettyPrint";
	public final static String MAP_ATTRIBUTES = "doc.map.attributesDefault";
	public final static String MAP_ELEMENTS = "doc.map.elementsDefault";
	public final static String MAP_UNCHECKED = "doc.map.unchecked";

	public final static String HIERARCHICAL_RESULT = "dml.query.tryHierarchical";
	public final static String PRESERVE_ENTITIES = "dml.query.preserveEntities";
	public final static String SPLIT_UPDATES = "dml.update.split";

	public final static String ROOT_NODE_VAR = "db";

	private Config() {
		// disallow instantiation of this class
	}

	static {
		prop = new Properties();
		try {
			prop.load(new FileReader("project.properties"));
		} catch (IOException e) {
			log.warn("Could not load project.properties, can only return default values.");
		}
	}

	public static long getLong(String key) {
		return getLong(key, 0);
	}

	public static long getLong(String key, long def) {
		return Long.parseLong(prop.getProperty(key, String.valueOf(def)));
	}

	public static int getInt(String key) {
		return getInt(key, 0);
	}

	public static int getInt(String key, int def) {
		return Integer.parseInt(prop.getProperty(key, String.valueOf(def)));
	}

	public static String getProperty(String key) {
		return getProperty(key, null);
	}

	public static String getProperty(String key, String def) {
		return prop.getProperty(key, def);
	}

	public static boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	public static boolean getBoolean(String key, boolean def) {
		return Boolean
				.parseBoolean(prop.getProperty(key, Boolean.toString(def)));
	}

	public static void setProperty(String key, String value) {
		prop.setProperty(key, value);
	}

	public static void setProperty(String key, Object value) {
		prop.setProperty(key, value.toString());
	}

	public static void setInt(String key, int value) {
		prop.setProperty(key, String.valueOf(value));
	}

	public static void setLong(String key, long value) {
		prop.setProperty(key, String.valueOf(value));
	}

	public static void setBoolean(String key, boolean value) {
		prop.setProperty(key, String.valueOf(value));
	}
}
